

import { GetInfoPicture, GetProjectArchivingInfo, GetPreviewImage, PostInfoPicture, PostProjectArchivingInfo, PostDeleteData } from "/constData/requestFun/_httpRequest";
import isEmpty from 'loadsh/isEmpty'
import { GetProjectManagement, GetProjectManagementById, PostProjectManagement, ProjectManagementDeleteData } from "/constData/requestFun/_teseRequest";
import { CORPID, SPACEID } from "/constData/requestFun/_globalData";
import compact from 'loadsh/compact'

Page({
  data: {
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    defaultDateRange: [new Date(), new Date()],
    arrIndex: 0,
    index: 0,
    projectinfo: {
      CorpId: '',
      ItemNumber: '',//项目编号
      ProjectName: '',//项目名称
      ProjectStartTime: new Date(),//项目开始时间
      ProjectEndTime: new Date(),//项目结束时间
      ResearchPersonnel: '',//研发人员
      ProjectBudget: '',//项目预算
      ProjectExecution: '',//项目实际执行数
      InitiationReport: '[]',//立项报告
      AnnualReport: '[]',//年度总结报告
      ClosingReport: '[]',//结项报告
      Annex: '[]',//	附件
    },
    ReceiveData: [],
    DisplayInitiation: [],//立项报告
    DisplayAnnual: [],//年度总结报告
    DisplayClosing: [],//结项报告
    DisplayAnnex: [],//	附件
  },
  onProjectExecutionChange(e) {
    this.setData({
      projectinfo: {
        ...this.data.projectinfo,
        ProjectExecution: e
      }
    })
  },
  onProjectBudgetChange(e) {
    this.setData({
      projectinfo: {
        ...this.data.projectinfo,
        ProjectBudget: e
      }
    })
  },
  onResearchPersonnelChange(e) {
    this.setData({
      projectinfo: {
        ...this.data.projectinfo,
        ResearchPersonnel: e
      }
    })
  },
  handleOk1(value, column, e) {
    this.setData({
      projectinfo: {
        ...this.data.projectinfo,
        ProjectStartTime:value,
        ProjectEndTime:value,
      },
    })
  },
  handleOk2(value, column, e) {
    this.setData({
      projectinfo: {
        ...this.data.projectinfo,
        ProjectEndTime:value
      }
    })
  },
  onItemNumberChange(e) {
    this.setData({
      projectinfo: {
        ...this.data.projectinfo,
        ItemNumber: e
      }
    })
  },
  onProjectNameChange(e) {
    this.setData({
      projectinfo: {
        ...this.data.projectinfo,
        ProjectName: e
      }
    })
  },

  Delete(e) {
    const id = e.currentTarget.dataset.id
    ProjectManagementDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },

  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetProjectManagement((res) => {
          const cacheData = res.data.map(it => {
            if (it.item1.corpId === storage.data.result.userid) {
              it.item1['DisplayInitiation'] = JSON.parse(it.item1.initiationReport)
              it.item1['DisplayAnnual'] = JSON.parse(it.item1.annualReport)
              it.item1['DisplayClosing'] = JSON.parse(it.item1.closingReport)
              it.item1['DisplayAnnex'] = JSON.parse(it.item1.annex)
              return it
            }
          })
          this.setData({
            projectinfo: {
              ...this.data.projectinfo,
              CorpId: storage.data.result.userid
            },
            ReceiveData: compact(cacheData).reverse()
          })
          console.log('this',this.data.ReceiveData);
        })
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    console.log('提交数据', JSON.stringify(this.data.projectinfo));
    PostProjectManagement(JSON.stringify(this.data.projectinfo),
      (res) => {
        my.alert({
          title: '提交成功',
        });
        GetProjectManagementById(res.data, (res) => {
          this.setData({
            ReceiveData: compact([...this.data.ReceiveData, res.data].map(it => {
              if (it.item1.corpId === this.data.projectinfo.CorpId) {
                it.item1['DisplayInitiation'] = JSON.parse(it.item1.initiationReport)
                it.item1['DisplayAnnual'] = JSON.parse(it.item1.annualReport)
                it.item1['DisplayClosing'] = JSON.parse(it.item1.closingReport)
                it.item1['DisplayAnnex'] = JSON.parse(it.item1.annex)
                return it
              }
            })).reverse()
          })
        })
        this.setData({
          projectinfo: {
            ...this.data.projectinfo,
            ItemNumber: '',//项目编号
            ProjectName: '',//项目名称
            ProjectStartTime: new Date(),//项目开始时间
            ProjectEndTime: new Date(),//项目结束时间
            ResearchPersonnel: '',//研发人员
            ProjectBudget: '',//项目预算
            ProjectExecution: '',//项目实际执行数
            InitiationReport: '[]',//立项报告
            AnnualReport: '[]',//年度总结报告
            ClosingReport: '[]',//结项报告
            Annex: '[]',//	附件
          }
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      })
  },
  //上传文件
  onUploadInitiationFile() {
    let initiationApprovalAnnex = ''
    dd.uploadAttachmentToDingTalk({
      types: ['file'],
      file: { spaceId: SPACEID, max: 9, folderId: '' },
      success: (res) => {
        const initiationReportAnnex = this.data.projectinfo.InitiationReport
        initiationApprovalAnnex = res.data
        initiationApprovalAnnex.push(...JSON.parse(initiationReportAnnex))
        this.setData({
          DisplayInitiation: initiationApprovalAnnex,
          projectinfo: {
            ...this.data.projectinfo,
            InitiationReport: JSON.stringify(initiationApprovalAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  //上传文件
  onUploadAnnualFile() {
    let annualAnnex = ''
    dd.uploadAttachmentToDingTalk({
      types: ['file'],
      file: { spaceId: SPACEID, max: 9, folderId: '' },
      success: (res) => {
        const annualReportAnnex = this.data.projectinfo.AnnualReport
        annualAnnex = res.data
        annualAnnex.push(...JSON.parse(annualReportAnnex))
        this.setData({
          DisplayAnnex: annualAnnex,
          projectinfo: {
            ...this.data.projectinfo,
            AnnualReport: JSON.stringify(annualAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  //上传文件
  onUploadClosingFile() {
    let closingAnnex = ''
    dd.uploadAttachmentToDingTalk({
      types: ['file'],
      file: { spaceId: SPACEID, max: 9, folderId: '' },
      success: (res) => {
        const closingReportAnnex = this.data.projectinfo.ClosingReport
        closingAnnex = res.data
        closingAnnex.push(...JSON.parse(closingReportAnnex))
        this.setData({
          DisplayClosing: closingAnnex,
          projectinfo: {
            ...this.data.projectinfo,
            ClosingReport: JSON.stringify(closingAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  onUploadFile() {
    let fileAnnex = ''
    dd.uploadAttachmentToDingTalk({
      types: ['file'],
      file: { spaceId: SPACEID, max: 9, folderId: '' },
      success: (res) => {
        const srcApprovalAnnex = this.data.projectinfo.Annex
        fileAnnex = res.data
        fileAnnex.push(...JSON.parse(srcApprovalAnnex))
        this.setData({
          DisplayAnnex: fileAnnex,
          projectinfo: {
            ...this.data.projectinfo,
            Annex: JSON.stringify(fileAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  PreviewBtn(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: CORPID,
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  CheckPictures(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: CORPID,
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
});